BSD TCP Server Sample

Introduction

This sample demonstrates how to write a TCP server application using the Berkeley Software Distribution (BSD) standard socket APIs to communicate with a SG15.

For the purpose of this demonstration, the server application creates a networking socket it uses to listen for incoming networking connections. A TCP connection is initiated by the SG15 to the sample application. Once the connection is established, the sample application attempts to transmit and receive data from the SG15.

SG15 Setup

            This sample was designed to operate with the SG15 operating in TCP Client mode.

            Configure the SG15

1.                Use the SG15 configuration tool to access the settings of this specific SG15.

2.                Choose the "Network" tab from the Configuration menu.

3.                Configure the module to function as a TCP client by doing the following:

a.                Check "TCP" and "Client" in the Connect Mode section.

b.                Enter the IP address of the PC running the sample in the "Remote IP" field.

c.                Specify 9101 as the TCP port number.

d.                Click "Apply now".

How To Build

This sample has been compiled and tested using GCC 3.2 on Red Hat Linux 8.0. It contains a Makefile that is used for building the application.

To build this sample

1.       Change directory to where the sample source files reside.

2.       Type make.

Step-by-Step

1.  Create a local socket

The call to create a socket looks like this:

 
int Socket = socket(AF_INET, Type, Protocol);
 

Where Type is SOCK_STREAM and Protocol is IPPROTO_TCP for a blocking, connection oriented TCP/IP socket.

2.  Bind to a local port

To bind the socket to a local port, call the following function:

 
bind(Socket, &SockAddr, sizeof(SockAddr));
 

Binding to a local port must be done before using the socket to calls like connect, and is used to associate the currently unnamed socket with a local name.

The SockAddr parameter is the struct sockaddr representation of the local name to bind to. This name consists of an address family (for TCP/IP, always AF_INET), a host address, and a port number.

In this sample, the calls to socket and bind are both done in the function MySocketOpen. Since this sample demonstrates a server application, a port (9101) is supplied to the call to MySocketOpen. The same port is also used by the SG15.

3.  Listen for incoming connections

Start listening for incoming connection requests by calling the listen function.

 
listen(ListenSocket, 1);
 

Calling listen puts the socket into a relatively inactive mode where it waits for incoming connection requests from a client. Connection requests are queued until processed in a "backlog". The size of this backlog is specified in the second parameter to the call to listen.

4.  Wait to accept an incoming connection

Call accept to wait for an incoming connection request.

 
int ConnectedSocket = accept(ListenSocket,
                             &AcceptSockAddr,
                             &AcceptSockAddrLength);
 

The sample is using blocking sockets, so the call to accept will not return until there is at least one connection request pending in the backlog queue. The socket value returned from accept represents the new connection that was just established. The listening socket remains in the passive listening mode, and the new connected socket should be used to communicate with the client.

5.  Transmit data

Transmitting data is done by calling send.

 
send(Socket, Data, Length, 0);
 

After accepting the incoming connection, the sample uses the socket to transmit some data.

For consistency the sample provides its own function named MySocketSend.

 
MySocketSend(MySocket, SendData, sizeof(SendData));
 

The parameters needed for MySocketSend are: MySocket which is the socket returned from MySocketOpen, SendData which is a pointer to the buffer holding the data to send, and sizeof(SendData) which is the number of bytes SendData points to.

6.  Receive data

To retrieve data received at the local address, call recv.

 
recv(Socket, Data, Length, 0);
 

With the SG15 setup in TCP Client mode, a scanned barcode will be sent directly to the sample application.

Again for consistency, the sample provides its own function, MySocketRecv

 
MySocketRecv(MySocket, RecvData, sizeof(RecvData));
 

The caller to MySocketRecv must supply the prerequisite MySocket, a buffer RecvData to hold the received data, and sizeof(RecvData) which specifies the length of RecvData.

8.  Close the connected socket

Accomplish this task by calling the following two functions:

 
shutdown(Socket, SHUT_RDWR);
close(Socket);
 

When the sample application is done using the socket, the connection it represents must be terminated properly (shutdown) and any resources it may be using should be released (close).

File List

hexdumpdata.cpp

hexdumpdata.h

Makefile

mysocket.cpp

mysocket.h

tcpserver.cpp

Keywords

socket; bind; listen; accept; send; recv; shutdown; close